package LUNCH;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.Collator;
import java.util.Locale;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;

public class CustJTable extends JTable {

	public static void main(String[] args) {
		Vector tableContVect1 = new Vector<String>();
		tableContVect1.add("列1");
		tableContVect1.add("列2");
		tableContVect1.add("列3");
		Vector colNameVect1 = new Vector<String>();
		colNameVect1.add("内容1");
		colNameVect1.add("内容2");
		colNameVect1.add("内容3");
		CustJTable t = new CustJTable(tableContVect1, colNameVect1, 300, 400,
				new int[] { 50, 50, 50 });
	}

	Vector tableContVect;
	Vector colNameVect;
	int length;
	int width;
	boolean[] flag;
	CustJTable table;
	int[] columnWidths;

	public CustJTable(Vector tableContVect, Vector colNameVect, int length,
			int width, int[] columnWidths) {
		this.tableContVect = tableContVect;
		this.colNameVect = colNameVect;
		this.length = length;
		this.width = width;
		this.columnWidths = columnWidths;
		this.table = this;
		initialize();
	}

	public Vector getTableContVect() {
		return tableContVect;
	}

	public void setTableContVect(Vector tableContVect) {
		this.tableContVect = tableContVect;
	}

	CustTableModel model = null;

	public CustTableModel getModel() {
		return model;
	}

	
	private void initialize() {
		model = new CustTableModel(this.tableContVect, this.colNameVect);
		setModel(model);
		setPreferredScrollableViewportSize(new Dimension(this.length,
				this.width));
		setAutoResizeMode(0);
		this.flag = new boolean[this.colNameVect.size()];
		for (int i = 0; i < this.colNameVect.size(); ++i) {
			this.flag[i] = false;
		}

		DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
		renderer.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);// 居中对齐
		for (int i = 0; i < getColumnCount(); ++i) {
			getColumnModel().getColumn(i).setPreferredWidth(columnWidths[i]);
			getColumnModel().getColumn(i).setCellRenderer(renderer);
		}
		getTableHeader().setReorderingAllowed(false);// 表列不可移动

	}
}